/*********************************************************************\
*
*                  (c) lkc
*                  All Rights Reserved. lkc Confidential
*
\*********************************************************************/
/*********************************************************************\
*   Title                      :
*
*   Module Description         :
*
*   Author                     : lkc
*
*   Created                    : 2019-11-8
*
\*********************************************************************/

#include "ProtocolHuaNuo.h"
#include "ImageDetect.h"
#include "UtilityTH.h"
#include "ProtocolPodXinWang.h"

ProtocolHuaNuo ProtocolHuaNuo::m_Instance;//静态成员变量在类中仅仅是声明，没有定义，所以要在类的外面定义，实际上是给静态成员变量分配内存。

int ProtocolHuaNuo::CycleSendOnce()
{
    int frameLenght = 100;
    unsigned char frameToSendCycle[frameLenght];
    memset(frameToSendCycle,0,frameLenght);
    frameToSendCycle[0] = HEAD1;
    frameToSendCycle[1] = HEAD2;
    frameToSendCycle[2] = DEV_ADDR;   //设备地址
    frameToSendCycle[3] = 0x00;   //Ox00 表示正常状态,Ox01 表示异常状态

    std::vector <cv::Point2d> points = ImageDetect::GetInstance().GetObjectPoints();
    unsigned int num = points.size();
    frameToSendCycle[4] = num;
    int i = 5;
    for(unsigned int j = 0; j < num; j++)
    {
        int xCode = static_cast<int>(points[j].x*10);
        int yCode = static_cast<int>(points[j].y*10);
        //x坐标，浮点数保留一位小数
        frameToSendCycle[i++] = xCode&0xFF;
        frameToSendCycle[i++] = (xCode>>8)&0xFF;
        //y坐标
        frameToSendCycle[i++] = yCode&0xFF;
        frameToSendCycle[i++] = (yCode>>8)&0xFF;
    }
    int count = i-2;
    frameToSendCycle[i++] = CheckSum(frameToSendCycle+2,count);       //校验和
    frameToSendCycle[i++] = 0xFB;       //帧尾
    int ret = Send(frameToSendCycle,i);
    if (ret < 0)
    {
        printf("ProtocolPod write data error!\n");
    }
    return ret;
}

////发送固定的目标信息用于测试
//int ProtocolHuaNuo::CycleSendOnce()
//{
//    int frameLenght = 100;
//    unsigned char frameToSendCycle[frameLenght];
//    memset(frameToSendCycle,0,frameLenght);
//    frameToSendCycle[0] = HEAD1;
//    frameToSendCycle[1] = HEAD2;
//    frameToSendCycle[2] = DEV_ADDR;   //设备地址
//    frameToSendCycle[3] = 0x00;   //Ox00 表示正常状态,Ox01 表示异常状态

//    std::vector <cv::Point2f> points = ImageDetect::GetInstance().GetObjectPoints();
//    unsigned int num = 2;
//    frameToSendCycle[4] = num;
//    int i = 5;
//    for(unsigned int j = 0; j < num; j++)
//    {
//        int xCode;
//        int yCode;

//        if(j==0)
//        {
//            xCode = 400*10;
//            yCode = 500*10;
//        }
//        else
//        {
//            xCode = 100*10;
//            yCode = 300*10;
//        }

//        //x坐标，浮点数保留一位小数
//        frameToSendCycle[i++] = xCode&0xFF;
//        frameToSendCycle[i++] = (xCode>>8)&0xFF;
//        //y坐标
//        frameToSendCycle[i++] = yCode&0xFF;
//        frameToSendCycle[i++] = (yCode>>8)&0xFF;
//    }
//    int count = i-2;
//    frameToSendCycle[i++] = CheckSum(frameToSendCycle+2,count);       //校验和
//    frameToSendCycle[i++] = 0xFB;       //帧尾
//    int ret = Send(frameToSendCycle,i);
//    if (ret < 0)
//    {
//        printf("ProtocolPod write data error!\n");
//    }
//    return ret;
//}


void ProtocolHuaNuo::ExtractData(const unsigned char *data, int nLength)
{
    ProtocolPodXinWang::GetInstance().Passthrough(data,nLength);
}

/********************************************************************\
 *
 * REVISION RECORDS
 *
\*********************************************************************/
/*********************************************************************/
/*
 *
 *
 *
\*********************************************************************/
/*------------------------------The End------------------------------*/
